package com.eims.sls_app.ui;

import com.eims.sls_app.fragment.CircleFragment;
import com.eims.sls_app.fragment.FollowFragment;
import com.eims.sls_app.fragment.HallFragment;
import com.eims.sls_app.fragment.LiveScoresFragment;
import com.eims.sls_app.fragment.MyCenterFragment;
import com.eims.sls_app.fragment.WinLotteryFragment;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.view.ConfirmDialog;
import com.eims.sls_app.view.MyToast;

import bls.juejin.cn.R;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class BetfairActivity extends MyFragmentActivity implements OnCheckedChangeListener {
	private static final String TAG = "BetfairActivity";
	private FrameLayout fl_content;
	private RadioGroup rg_tab;
	private RadioButton rb_info;
	private int position;
	private String[] title;
	Fragment mFragment = null;
	private ConfirmDialog dialog;// 提示框
	private FragmentManager mFragmentManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_betfair);
		initView();
	}
	@Override
	public Resources getResources() {
		// TODO Auto-generated method stub
		Resources res = super.getResources();
		Configuration config = new Configuration();
		config.setToDefaults();
		res.updateConfiguration(config, res.getDisplayMetrics());
		return res;
	}
	private void initView() {
		fl_content = (FrameLayout) findViewById(R.id.fl_content);
		rg_tab = (RadioGroup) findViewById(R.id.rg_tab);
		rb_info = (RadioButton) findViewById(R.id.rb_info);
		Bundle bundle = getIntent().getExtras();
		position = bundle.getInt("position");
		title = bundle.getStringArray("title");
		rb_info.setText(title[position]);
		MyToast.getToast(this, title[position]);
		rb_info.setChecked(true);
		switchFragment(getInstanceByIndex(R.id.rb_info));
		mFragmentManager = getSupportFragmentManager();
		rg_tab.setOnCheckedChangeListener(this);
		
	}

	@Override
	public void onCheckedChanged(RadioGroup arg0, int arg1) {
		Log.i(TAG, "arg"+arg1);
		if (arg1 == 2131099804) {
			if (null == App.user) {
				Intent intent = new Intent(BetfairActivity.this, LoginActivity.class);
				startActivity(intent);
				
			}else {
				switchFragment(getInstanceByIndex(arg1));
			}
		}else {
			switchFragment(getInstanceByIndex(arg1));
		}
		
		
	}
	
	private void switchFragment(Fragment fragment) {
		if (fragment != mFragment) {
			if (!fragment.isAdded()) {
				getSupportFragmentManager().beginTransaction()
						.add(R.id.fl_content, fragment)
						.commitAllowingStateLoss();
			} else {
				getSupportFragmentManager().beginTransaction().hide(mFragment)
						.show(fragment).commitAllowingStateLoss();
			}
			mFragment = fragment;
		}
	}
	
	 public Fragment getInstanceByIndex(int index) {  
	        Fragment fragment = null;  
	        switch (index) {  
	            case R.id.rb_hall:  
	                fragment = new HallFragment();  
	                break;  
	            case R.id.rb_info:  
	                //fragment = LiveScoresFragment.newInstance(position);  
	                break;  
	            case R.id.rb_follow:  
	                fragment = new CircleFragment();  
	                break;  
	            case R.id.rb_win:  
	                fragment = new WinLotteryFragment();  
	                break;  
	            case R.id.rb_center:  
	            	
						fragment = new MyCenterFragment();  
					
	                
	                break; 
	        }  
	        return fragment;  
	    } 
	 
	 private long firstTime=0;
	 /**
		 * 重写返回键事件
		 */
		@Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			// TODO Auto-generated method stub
			if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN) {
				 if (System.currentTimeMillis()-firstTime>2000){
		                Toast.makeText(BetfairActivity.this,"再按一次退出"+getText(R.string.app_name)+"程序",Toast.LENGTH_SHORT).show();
		                firstTime=System.currentTimeMillis();
		            }else{
		                finish();
		                System.exit(0);
		            }
		            return true;
			}
			return super.onKeyDown(keyCode, event);
		}
		
		@Override
		protected void onStart() {
			// TODO Auto-generated method stub
			super.onStart();
			rb_info.setChecked(true);
			switchFragment(getInstanceByIndex(R.id.rb_info));
		}
		
		@Override
		protected void onPause() {
			// TODO Auto-generated method stub
			super.onPause();
			rb_info.setChecked(true);
			switchFragment(getInstanceByIndex(R.id.rb_info));
		}

}
